/**
 * 
 * 深圳市店联电子商务有限公司
 * 
 * 
 * Copyright (c) 2011-2012 RUIPENGKJ.Co.Ltd. All rights reserved.
 * 
 * */
package com.ruipengkj.view.baseDataView;

import java.awt.FileDialog;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.IOException;
import java.util.Date;

import javax.imageio.ImageIO;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.KeyStroke;

import com.ruipengkj.bean.TSaleactSummary;
import com.ruipengkj.commons.BuildSpringApplication;
import com.ruipengkj.commons.util.DateFormatUtil;
import com.ruipengkj.commons.util.FileUtil;
import com.ruipengkj.commons.util.Util;
import com.ruipengkj.component.DataPackageComponent;
import com.ruipengkj.component.JLabelJTextAreaComponent;
import com.ruipengkj.component.JLabelJTextFieldComponent;
import com.ruipengkj.component.JPanelComponent;
import com.ruipengkj.service.TSaleactSummaryService;
import com.ruipengkj.view.base.BaseDialog;
import com.ruipengkj.view.systemView.SelectUserPanel;

/**
 * 说明：
 * 增加自定义Dialog
 * @Author：朱攀
 * @Date：2012-07-18
 */
public class SaleactUpdateDialog extends BaseDialog {


	
	private static final long serialVersionUID = 1L;
	
	private JButton okButton;//确定
	private JButton cButton;//取消
	private JLabelJTextFieldComponent actID=null;//满就送编号
	private JLabelJTextFieldComponent title=null;//标题
	private JPanelComponent SmallPic;//小图片
	private	DataPackageComponent starTime;//有效期INTEGER1为天 2为月 3为年
	private	DataPackageComponent endTime;//有效期INTEGER1为天 2为月 3为年
	private JLabelJTextAreaComponent Des; //描述
	private JButton uploadImageButton; //上传图片按钮
	private String path;//保存图片路径
	private TSaleactSummary tSaleactSummary;
	
	public boolean isSuccess=false;  //判断是否成功
	private  TSaleactSummaryService  tSaleactSummaryService;
	public SaleactUpdateDialog(JDialog jd, String title, boolean b) {
		super(jd, title, b);
		tSaleactSummaryService = BuildSpringApplication.getInstance().getTSaleactSummaryServiceImpl();
		this.init();
		this.setSize(400, 650);
		this.setLocationRelativeTo(null);
		this.setResizable(false);
	}
	private void init(){
		this.initViews();
		
		this.initListener();
	}
	
	private void initViews(){
		JPanel main_jp=new JPanel();
		BoxLayout boxLayout=new BoxLayout(main_jp,BoxLayout.Y_AXIS);
		main_jp.setLayout(boxLayout);
		
		main_jp.setBackground(Util.PANEL_COLOR);
		
		
		actID=new JLabelJTextFieldComponent("编号：",30);
		//actID.getText().setText(UUID.GenUUID15());  //设置默认编号
		actID.getText().setEditable(false);   //把文本框设为只读
		title = new JLabelJTextFieldComponent("标题：", 30);
		Des = new JLabelJTextAreaComponent(" 描述：", 22,3);;//描述
		starTime = new DataPackageComponent("开始时间：", 20); //开始时间
		endTime= new DataPackageComponent("结束时间", 20);;// 结束时间

		SmallPic=new JPanelComponent(null, 100, 100);  //图片
		uploadImageButton=new JButton("上传图片");
		
		JPanel jp1=new JPanel();
		okButton=new JButton("保存");
		cButton=new JButton("关闭");
		jp1.add(okButton);
		jp1.add(cButton);
		
		JPanel jp2=new JPanel();
		jp2.add(actID);
		jp2.add(title);
		jp2.add(Des);
		jp2.add(starTime);
		jp2.add(endTime);
		jp2.add(uploadImageButton);
		jp2.add(SmallPic);
		jp1.setOpaque(false);
		jp2.setOpaque(false);
		main_jp.add(jp2);
		main_jp.add(jp1);
		
		this.add(main_jp);
	}
	private void initListener(){
		uploadImageButton.addActionListener(this);
		okButton.addActionListener(this);
		okButton.registerKeyboardAction(this, KeyStroke.getKeyStroke(KeyEvent.VK_C, KeyEvent.CTRL_MASK), JComponent.WHEN_IN_FOCUSED_WINDOW);
		
		cButton.addActionListener(this);
		cButton.registerKeyboardAction(this, KeyStroke.getKeyStroke(KeyEvent.VK_Q, KeyEvent.CTRL_MASK), JComponent.WHEN_IN_FOCUSED_WINDOW);
	}

	public void showDialog(TSaleactSummary saleact){
		if(saleact!=null){
			
			
			
			actID.getText().setText(saleact.getActID());
			title.getText().setText(saleact.getTitle());
			Des.getArea().setText(saleact.getDes());
			path=saleact.getMainPic();//小图片
			SmallPic.setImage(path);
			starTime.setDate(saleact.getStartTime());
			endTime.setDate(saleact.getEndTime());
			
			
		}
		this.setVisible(true);
	}
	
	public void actionPerformed(ActionEvent e) {
		
		
		if(e.getSource()==okButton){
			//提交
			Byte gtype=0;
			String actid=actID.getText().getText().trim();//编号
			String atitle=title.getText().getText().trim(); //标题
			String DesStr=Des.getArea().getText().trim();//商品描述
			Date stime=starTime.getDate();//有效期INTEGER1为天 2为月 3为年
			Date etime = endTime.getDate();
			
			
			
			if(stime==null){
				stime=new Date();
			}
			
			if(etime==null){
				etime=new Date();
			}
			
			
			
			if(atitle.equals("")||atitle.length()>20){
				JOptionPane.showMessageDialog(null, "标题不能为空，且不能大于20个字");
				title.getText().requestFocus();
				return;
			}
			
			if(DesStr.equals("")||DesStr.length()>400){
				JOptionPane.showMessageDialog(null, "描述不能为空，且不能大于20个字");
				title.getText().requestFocus();
				return;
			}
			
			
			
			Date date=new Date();
			//TSendCardRecord tSendCardRecord=new TSendCardRecord();
			//tSendCardRecord.setSendCardRecordID(UUID.GenUUID15());
			//tSendCardRecord.setCardNum(cardNum);
			//tSendCardRecord.setSendCardTime(date);
			TSaleactSummary saleact =new  TSaleactSummary();
			saleact.setActID(actid);
			saleact.setTitle(atitle);
			saleact.setDes(DesStr);
			saleact.setStartTime(stime);
			saleact.setEndTime(etime);
			saleact.setUpdateTime(stime);
			saleact.setType(6);
			if (path != null) {
				if (!FileUtil.existImage(path)) {
					JOptionPane.showMessageDialog(null, "路径"+path+"图片不存在或被删除！");
					SmallPic.setImage(null);
					SmallPic.updateUI();
					path = null;
					return;
				}
				String path2 = "goodsImage/"+DateFormatUtil.formatI(new Date())+".jpg";
				FileUtil.copyFile(new File(path), new File(path2));
				saleact.setMainPic(path2);//小图片
			}
			
			
			//tUser.getTSendCardRecords().add(tSendCardRecord);
			try {
				
				tSaleactSummaryService.saveOrUpdateService(saleact);  // 掉用增加方法
				BuildSpringApplication.getInstance().getTOpLogServiceImpl().addOpLogService("增加自定义优惠，编号号为：+"+actID);
				JOptionPane.showMessageDialog(null, "操作成功");
				SelectUserPanel s=new SelectUserPanel();
				isSuccess=true;
				this.dispose();
				 
			} catch (Exception e1) {
				e1.printStackTrace();
				JOptionPane.showMessageDialog(null, "操作失败，请重新操作");
				isSuccess=false;
				
			}
		}else if(e.getSource() == uploadImageButton) {
			path = selectFile("选择商品图片");
			if (path != null && !path.equals("")) {
				SmallPic.setImagePath(path);
			}
		
		}else if(e.getSource() == cButton) {
			if(Util.showConfirmDialog("是否要取消？")==0){
				isSuccess=false;
				this.dispose();
			}
		}
	}
	
	
	
	
	// 选择本地文件
	private String selectFile(String title) {
		FileDialog fileChooser = null;
		fileChooser = new FileDialog(new JFrame(), title);
		fileChooser.setVisible(true);
		if (fileChooser.getDirectory()!= null && fileChooser.getFile() != null) {
			String path = fileChooser.getDirectory() + fileChooser.getFile();
			if (path != null) {
				File file = new File(path);
				Image image = null;
				try {
					image = ImageIO.read(file);
				} catch (IOException e) {
					e.printStackTrace();
				}
				if(image == null) {
					JOptionPane.showMessageDialog(null, "请确认你所选择的是图片");
					return null;
				} else{
					return path;
				}
			}
		}
		return null;
	}
	

	
//	public static void main(String[] args)
//	{
//		JFrame f = new JFrame();
//		f.setSize(300,300);
//		f.setTitle("选择商品");
//		JComboBox c = new CommodityJComboBoxTableComponent();
//		f.getContentPane().add(c);
//		f.setVisible(true);
//	}
}
